package de.baumann.browser.activitys.oc

import android.content.Context
import android.content.Intent
import de.baumann.browser.R
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.db.ETHWallet
import de.baumann.browser.iview.ICreateWalletView
import de.baumann.browser.present.CreateWalletPresenter
import de.baumann.browser.utils.web3j.OdinWallet
import kotlinx.android.synthetic.main.activity_oc_create_wallet.*
import kotlinx.android.synthetic.main.include_tool_bar.*


class OcCreateWalletActivity : BaseOdinActivity<ICreateWalletView, CreateWalletPresenter>(), ICreateWalletView {

    companion object {
        fun startOcCreateWalletActivity(context: Context) {
            context.startActivity(Intent(context, OcCreateWalletActivity::class.java))
        }
    }

    override fun getLayout(): Int {
        OdinWallet.addAtivity(this)
        return R.layout.activity_oc_create_wallet
    }

    override fun getTitleText(): String {
        return "创建钱包"
    }

    override fun initView() {
        initToolbar(commToolBar, commTitle)
        btnWalletCreate.setOnClickListener {
            if (getWalletName().isEmpty()) {
                showShortToast("请输入钱包名称")
                return@setOnClickListener
            }

            if (getWalletPwd().isEmpty()) {
                showShortToast("请输入钱包密码")
                return@setOnClickListener
            }

            if (getOcWalletRePwd().isEmpty()) {
                showShortToast("请确认密码")
                return@setOnClickListener
            }

            if (getWalletPwd() != getOcWalletRePwd()) {
                showShortToast("两次输入的密码不一致")
                return@setOnClickListener
            }
            showLoading()
            presenter.createWallet()
        }
    }


    override fun initData() {
    }

    override fun onDestroy() {
        super.onDestroy()
        OdinWallet.removeActivity(this)
        hideLoading()
    }

    private fun getOcWalletDesc(): String {
        return edOcWalletDesc.text.toString().trim()
    }

    override fun createPresenter(): CreateWalletPresenter {
        return CreateWalletPresenter()
    }

    override fun createView(): ICreateWalletView {
        return this
    }

    override fun getWalletName(): String {
        return edOcWalletName.text.toString().trim()
    }

    override fun getWalletPwd(): String {
        return edOcWalletPwd.text.toString().trim()
    }

    private fun getOcWalletRePwd(): String {
        return edOcWalletRePwd.text.toString().trim()
    }

    override fun jumpToWalletBackUp(wallet: ETHWallet) {
        hideLoading()
        OcBackupTipsActivity.startOcBackupTipsActivity(mContext, wallet)
    }

    override fun showBindEth(): Boolean {
        return false
    }
}
